package com.pan.service.impl;

import com.pan.dao.goodsPriceHistory.GoodsPriceHistoryDao;
import com.pan.dao.category.CategorySelectDao;
import com.pan.dao.category.CategoryUpdateDao;
import com.pan.dao.goods.GoodsDao;
import com.pan.dao.goods.GoodsSelectDao;
import com.pan.dao.goods.GoodsUpdateDao;
import com.pan.entity.Goods;
import com.pan.entity.Result;
import com.pan.enume.ResultEnum;
import com.pan.service.GoodsService;
import com.pan.tools.ConvertObject;
import com.pan.vo.GoodsCreateVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;


@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsSelectDao goodsSelectDao;

    @Resource
    private GoodsDao goodsDao;



    @Resource
    private CategoryUpdateDao categoryUpdateDao;

    @Resource
    private CategorySelectDao categorySelectDao;

    @Resource
    private GoodsUpdateDao goodsUpdateDao;

    @Resource
    private GoodsPriceHistoryDao goodsPriceHistoryDao;


    @Override
    public Result<List<Goods>> selectGoodsList(List<Long> goodsIds){
        List<Goods> li = goodsDao.selectGoodsListByIdList(goodsIds);
        Result<List<Goods>> result = new Result(ResultEnum.SUCCESS)
                .setData(li);
        return result;
    }

    @Override
    public Result<List<Goods>> selectGoodsListPage(Integer start,Integer end){
        List<Goods> li = goodsDao.selectPage(start,end);
        Result<List<Goods>> result = new Result<>(ResultEnum.SUCCESS,li);
        return result;
    }

    @Override
    public Result<Goods> selectById(Long id){
        Goods goods = goodsSelectDao.selectById(id);
        if (goods==null){
            return new Result<>(ResultEnum.NOT_FOUND_ERROR);
        }
        Result<Goods> result = new Result<>(ResultEnum.SUCCESS,goods);
        return result;
    }

    @Override
    public Result<List<Goods>> selectByCategory(Long categoryId){
        List<Goods> li = goodsSelectDao.selectByCategoryId(categoryId);
        if (li==null){
            return new Result<>(ResultEnum.NOT_FOUND_ERROR);
        }
        Result<List<Goods>> result = new Result<>(ResultEnum.SUCCESS,li);
        return result;
    }

    @Override
    public Result<List<Goods>> selectByRandom(Integer count){
        if (count > 10){
            return new Result<>(403,"获取数量过多");
        }
        Integer goodsCount = goodsDao.getCount();
        Random random = new Random();
        Set<Integer> set = new HashSet<>();
        while (set.size()<goodsCount){
            set.add(random.nextInt(goodsCount));
        }
        List<Goods> li = new ArrayList<>();
        set.stream().forEach(i->li.add(goodsSelectDao.selectById(i.longValue())));
        Result result = new Result<>(ResultEnum.SUCCESS,li);
        return  result;
    }

    @Transactional
    @Override
    public Result insert(String adminTicket, GoodsCreateVo goodsCreateVo){

        Goods goods = (Goods) ConvertObject.convertObject(Goods.class,goodsCreateVo);
        goods.setGoodsStatus(0);
        Timestamp nowTime = new Timestamp(System.currentTimeMillis());
        goods.setCreateDate(nowTime);
        goods.setUpdateDate(nowTime);
        goods.setSaleCount(0L);
        Long id = goodsDao.insert(goods);
        Result result = new Result<>(ResultEnum.SUCCESS,id);
        return result;
    }


    @Override
    @Transactional
    public Result update(String adminTicket,Long id,Object updateValue,String updateMemberName) {


        Field[] fields = GoodsCreateVo.class.getDeclaredFields();
        List<String> li = Arrays.stream(fields).map(Field::getName)
                .filter(name -> name.equals(updateMemberName))
                .collect(Collectors.toList());
        if (li.size()==0||li==null){
            return new Result(403,"查无此属性",updateMemberName);
        }
        String name = li.get(0);
        Field field = null;
        try {
            field = Goods.class.getDeclaredField(name);
            field.setAccessible(true);
            Goods goods = goodsSelectDao.selectById(id);
            field.set(goods,name);
            Class updateType = field.getType();
            String updateMethodName = "update"+name.substring(0,1).toUpperCase()+name.substring(1);
            Method updateMethod = GoodsUpdateDao.class.getMethod(updateMethodName,Long.class,updateType);
            updateMethod.invoke(goodsUpdateDao,id,updateValue);
        } catch (IllegalAccessException | NoSuchFieldException | NoSuchMethodException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return new Result(ResultEnum.SUCCESS);
    }

}
